﻿using System;
using System.Data;
namespace E2_WEBSITE.Code.Admin
{
    public class UserInfo
    {
        public int ID { get; set; }
        public string Email { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public bool Gender { get; set; }
        public DateTime BirthDay { get; set; }
        public string Occupation { get; set; }
        public string Phone { get; set; }
        public int Score { get; set; }
        public int Type { get; set; }
        public DateTime LastAccess { get; set; }
        public string LoginName { get; set; }

        public UserInfo()
        { }

        public UserInfo(
            string name,
            string email,
            string address,
            bool gender,
            DateTime birth,
            string occupation,
            string phone,
            string login,
            int type)
        {
            this.Name = name;
            this.Email = email;
            this.Address = address;
            this.Gender = gender;
            this.BirthDay = birth;
            this.Occupation = occupation;
            this.Phone = phone;
            this.LoginName = login;
            this.Type = type;
        }

        static public UserInfo Load(DataTable data)
        {
            try
            {
                UserInfo user = new UserInfo();

                user.ID = int.Parse(data.Rows[0]["ID"].ToString());
                user.Email = data.Rows[0]["Email"].ToString();
                user.Name = data.Rows[0]["Ten"].ToString();
                user.Address = data.Rows[0]["DiaChi"].ToString();
                user.Gender = bool.Parse((data.Rows[0]["GioiTinh"].ToString()));
                user.BirthDay = DateTime.ParseExact(data.Rows[0]["NgaySinh"].ToString(), Utilities.DateTimeFormatString, null);
                user.Occupation = data.Rows[0]["NgheNghiep"].ToString();
                user.Phone = data.Rows[0]["DienThoai"].ToString();
                user.Score = int.Parse(data.Rows[0]["Diem"].ToString());
                user.Type = int.Parse(data.Rows[0]["LoaiNguoiDung"].ToString());
                user.LastAccess = DateTime.ParseExact(data.Rows[0]["DangNhapTruoc"].ToString(), Utilities.DateTimeFormatString, null);
                user.LoginName = data.Rows[0]["TenDangNhap"].ToString();

                return user;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override bool Equals(object obj)
        {
            if (obj is UserInfo)
            {
                UserInfo u = obj as UserInfo;
                return (this.Name.Equals(u.Name) &&
                        this.Email.Equals(u.Email) &&
                        this.Address.Equals(u.Address) &&
                        this.Gender.Equals(u.Gender) &&
                        this.BirthDay.Equals(u.BirthDay) &&
                        this.Occupation.Equals(u.Occupation) &&
                        this.Phone.Equals(u.Phone) &&
                        this.LoginName.Equals(u.LoginName));
            }
            else
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}
